#include "denglu.h"
#include "ui_denglu.h"
#include "zhuce.h"
#include "yanzhengdenglu.h"
#include "wangjimima.h"
#include "sql.h"
#include<QMessageBox>
#include<QDebug>
#include <QLineEdit>

DengLu::DengLu(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::DengLu)
{
    ui->setupUi(this);


    // 创建一个标签用于显示背景图像
        QLabel *backgroundLabel = new QLabel(this);
        QPixmap backgroundImage(":/new/prefix1/bjt.png"); // 加载背景图像
        backgroundLabel->setPixmap(backgroundImage);
        backgroundLabel->setScaledContents(true); // 缩放背景图像以填充标签

        // 设置标签的位置和大小以覆盖整个窗口
        backgroundLabel->setGeometry(0, 0, this->width(), this->height());

        // 将标签放置在窗口的底层
        backgroundLabel->lower();


    zhuce = new ZhuCe();
    yanzhengdenglu = new YanZhengDengLu();
    wangjimima = new WangJiMiMa();

    connect(zhuce, &ZhuCe::backToLogin, this, [=]() {
        this->show();          // 显示登录界面
        zhuce->hide();  // 隐藏注册界面
    });
    connect(yanzhengdenglu, &YanZhengDengLu::backToLogin, this, [=]() {
        this->show();          // 显示登录界面
        yanzhengdenglu->hide();  // 隐藏界面
    });
    connect(wangjimima, &WangJiMiMa::backToLogin, this, [=]() {
        this->show();          // 显示登录界面
        wangjimima->hide();  // 隐藏界面
    });
}

DengLu::~DengLu()
{
    delete ui;
    delete zhuce; // 释放注册界面实例
    delete yanzhengdenglu;
    delete wangjimima;

}

void DengLu::on_yanzhengmadenglu_clicked()
{
    yanzhengdenglu->show();
    this->hide();
}


void DengLu::on_denglu_2_clicked()
{
    QLineEdit *lineEditUsername = findChild<QLineEdit*>("zhanghao");
    QLineEdit *lineEditPassword = findChild<QLineEdit*>("mima");

    QString username = lineEditUsername->text();
    QString password = lineEditPassword->text();

    if (SqlController::getInstance()->goLogin(username, password)) {


      SqlController::getInstance()->getUsername(username, LAS);
               qDebug()<<"登录信息已保存";

        // 登录成功，显示主窗口
        MainWindow *userView = new MainWindow();
        userView->show();
        this->hide();
    } else {
        qDebug() << "登陆失败";
        QMessageBox::warning(this, "登录失败", "用户名或密码错误");

        lineEditUsername->clear();
        lineEditPassword->clear();

        m_loginAttempts++;

        if (m_loginAttempts >= 5) {
            qDebug() << "尝试次数已达上限，锁死输入框";
            lineEditUsername->setDisabled(true);
            lineEditPassword->setDisabled(true);
            QMessageBox::warning(this, "错误次数过多", "您已连续输入错误密码超过5次，请稍后再试或联系管理员。");
        }
    }
}


void DengLu::on_zhuce_clicked()
{
    zhuce->show();
    this->hide();
}


void DengLu::on_wangjimima_clicked()
{
    wangjimima->show();
    this->hide();
}

